package com.tanhua.server.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.tanhua.server.service.TestSoulListService;
import com.tanhua.server.vo.TestReportVo;
import com.tanhua.server.vo.TestSoulListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("testSoul")
public class TestSoulController {
    @Autowired
    private TestSoulListService testSoulListService;

    @GetMapping
    public ResponseEntity<Object> getTestSoulList(){
        List<TestSoulListVo> testSoulList = testSoulListService.getTestSoulList();
        if (CollUtil.isEmpty(testSoulList)){
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
        return ResponseEntity.ok(testSoulList);
    }

    @PostMapping
    public ResponseEntity<Object> recordTestSoulAnswers(@RequestBody Map<String, Object[]> map){
        String reportId = testSoulListService.recordTestSoulAnswers(map);
        if (ObjectUtil.isEmpty(reportId)){
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
        return ResponseEntity.ok(reportId);
    }

    @GetMapping("/report/{id}")
    public ResponseEntity<Object> getTestReport(@PathVariable("id") Long reportId){
        TestReportVo testReportVo = testSoulListService.getTestReport(reportId);
        if (ObjectUtil.isEmpty(testReportVo)){
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
        return ResponseEntity.ok(testReportVo);
    }


}
